package stack;
import java.util.Arrays;
public class MyStack {
    public int[] array;
    public int usedSize;
    public static final int CAPACITY = 2;
    public MyStack(){
        this.array = new int[CAPACITY];
    }
    //增容
    private void addCapacity(){
        array = Arrays.copyOf(array, array.length * 2);
    }
    //压栈
    public void push(int val){
        if(usedSize == CAPACITY){
            addCapacity();
        }
        array[usedSize++] = val;
    }
    //查看栈顶元素
    public int peek(){
        if(usedSize == 0){
            throw new StackNull("栈为空!");
        }
        return array[usedSize - 1];
    }
    //出栈
    public int pop(){
        if(usedSize == 0){
            throw new StackNull("栈为空!");
        }
        int ret = array[usedSize - 1];
        usedSize--;
        return ret;
    }
}
